package com.zhangruile.web.servlet;

import com.zhangruile.service.TrainService;
import com.zhangruile.service.TrainServiceImp;
import com.zhangruile.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * ClassName: TrainServlet
 *
 * @author :张蕊乐
 *         version :
 * @since JDK 1.8
 * date: 2017 11 10 10:07
 */
@WebServlet("/train/*")
public class TrainServlet extends BaseServlet {
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        //处理中文
        request.setCharacterEncoding("utf-8");
        //得到参数
        String trainNum=request.getParameter("trainNum");
        String startStation=request.getParameter("startStation");
        String endStation=request.getParameter("endStation");
        //判断合法性

        if (trainNum==null || "".equals(trainNum)){
            request.setAttribute("msg","站次不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }

        if (startStation==null || "".equals(startStation)){
            request.setAttribute("msg","起始站不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }

        if (endStation==null || "".equals(endStation)){
            request.setAttribute("msg","终点站不能为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }


        //调用业务方法

        TrainService trainService=new TrainServiceImp();
        Map<String,Object> map=trainService.getPrice(trainNum,startStation,endStation);
        //将查询结果放到session中
        request.setAttribute("result",map);
        request.getRequestDispatcher("/WEB-INF/views/show.jsp").forward(request,response);
    }

}
